from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from module_admin.entity.vo.cms_car_vo import CarModel, CarPageObject, CarForQuery, CarModelForPage, \
    OrderListMessageForQuery, CarModelForRecordList
from module_admin.entity.vo.cms_driver_vo import OrderListForQuery
from module_admin.entity.vo.cms_order_vo import OrderPlanTime
from module_admin.service.cms_car_service import CmsCarService
from module_admin.dao.dept_dao import *
from module_admin.service.login_service import get_current_user_details
from utils.page_util import get_page_obj, get_new_page_obj
from utils.response_util import *
from utils.log_util import *
from utils.time_format_util import object_format_datetime

carController = APIRouter(dependencies=[Depends(get_current_user_details)])


@carController.post("/record/create", response_model=CarModel, summary="车辆档案-新增")
async def create_car(request: Request, query: CarModel, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.create_car(query_db, query)
        return response_200(data=dept_query_result, message="新增成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.get("/record/delete/{car_id}", summary="车辆档案-删除")
async def delete_car(request: Request, car_id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.delete_car(query_db, car_id)
        return response_200(data=dept_query_result, message="删除成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.post("/record/update", summary="车辆档案-编辑")
async def update_car(request: Request, car_update: CarModel, db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.update_car(db, car_update)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.get("/record/get/{id}", summary="车辆档案-详情")
async def get_car(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.get_car(query_db, id)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.get("/record/getNew/{id}", summary="车辆档案-详情(实时信息 档案信息 统计信息)")
async def get_car_new(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.get_car_new(query_db, id)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.post("/record/getNewOrderList/", summary="车辆档案-详情(任务信息)")
async def get_car_new(request: Request, order: OrderListMessageForQuery, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.get_car_new_order_list(query_db, order)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.post("/record/page", response_model=CarModelForPage, summary="车辆档案-分页")
async def page_car(request: Request, car_page_query: CarPageObject, db: Session = Depends(get_db)):
    try:
        page_result = CmsCarService.page(db, car_page_query)
        total = CmsCarService.count(db, car_page_query)
        list_format_datetime(page_result)
        # 分页操作
        page_result_after = get_new_page_obj(page_result, car_page_query.page_num, car_page_query.page_size,
                                             total)
        # 处理
        return response_200(data=page_result_after, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.get("/record/list/{id}", response_model=CarModelForRecordList,
                   summary="车辆档案-空闲车辆下拉框，id为用车申请数字id")
async def record_list(request: Request, order_id: int, db: Session = Depends(get_db)):
    try:
        result = CmsCarService.list_free(db, order_id)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.post("/record/listForUrgent", summary="车辆档案-空闲车辆下拉框(紧急)")
async def record_list_for_urgent(request: Request, order_plan_time: OrderPlanTime, db: Session = Depends(get_db)):
    try:
        result = CmsCarService.list_free_for_urgent(db, order_plan_time)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.get("/record/listAll", summary="车辆档案-全部车辆下拉框 此处order_id_num为纯数字id")
async def record_list_all(request: Request, order_id_num: int = 0, db: Session = Depends(get_db)):
    try:
        result = CmsCarService.list_all(db, order_id_num)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.post("/record/listAllForUrgent", summary="车辆档案-全部车辆下拉框(紧急)")
async def record_list_all_for_urgent(request: Request, order_plan_time: OrderPlanTime, db: Session = Depends(get_db)):
    try:
        result = CmsCarService.list_all_for_urgent(db, order_plan_time)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carController.post("/record/orderList", summary="车辆档案-任务列表")
async def page_driver(request: Request, car_order_query: OrderListForQuery, db: Session = Depends(get_db)):
    try:
        result = CmsCarService.order_list(db, car_order_query)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
